﻿package kr.pfg.pfgTracer.view.list
{
	import caurina.transitions.Tweener;
	
	import display.Align;
	import display.CTextField;
	import display.CTextFormat;
	import display.data.CTextFieldType;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import kr.pfg.pfgTracer.model.event.VListItemSelectEvent;
	import kr.pfg.pfgTracer.model.vo.EchoClient;
	
	import skinfactory.SkinImage;
	
	public class VListItem extends Sprite
	{
		private var bg: Sprite;
		private var txtField: CTextField;
		
		private var key: String;
		
		private var icon: SkinImage;
		public function VListItem()
		{
			init();
		}
		private function init(): void
		{
			bg = new Sprite();
			bg.graphics.beginFill( 0xFFFFFF );
			bg.graphics.drawRect( 0, 0, 10 , 10 );
			bg.alpha = 0;
			bg.buttonMode = true;
			bg.addEventListener( MouseEvent.MOUSE_OVER, mouseHandler );
			bg.addEventListener( MouseEvent.MOUSE_OUT, mouseHandler );
			bg.addEventListener( MouseEvent.CLICK, clickHandler );
			
			txtField = new CTextField( CTextFieldType.LIST );
			txtField.defaultTextFormat = CTextFormat.getInstance().list;
			txtField.autoSize = TextFieldAutoSize.NONE;
			txtField.height = 20;
			
			icon = new SkinImage( "list_icon" );
			
			this.setPosition();
			this.initChild();
		}
		private function setPosition(): void
		{
			icon.x = 1;
			icon.y = 2;
			
			txtField.x = Align.getPosX( icon, 1 );
		}
		private function initChild(): void
		{
			this.addChild( bg );
			this.addChild( txtField );
			this.addChild( icon );
		}
		override public function set width(value:Number):void
		{
			bg.width = value;
			txtField.width = value - 10;
		}
		override public function set height(value:Number):void
		{
			bg.height = value;
		}
		override public function get width():Number
		{
			return bg.width;
		}
		override public function get height():Number
		{
			return bg.height;
		}
		/*
		//	name = 파일명;
		//	version = player 버전
		//	frame = 프레임수
		//	actionV = 액션스크립트 버전 			
		*/
		
		private var ballow: VListItemBallow;
		public function setData( data: EchoClient ): void
		{
			key = data.key;
			txtField.text = data.swfMember[0];
			ballow = new VListItemBallow();
			ballow.optionData( data.swfMember );
			addChild ( ballow );
			ballow.y = 10;
			ballow.x = 10;
			ballow.visible = false;
		}
		
		private function clickHandler ( e: MouseEvent ): void
		{
			var data: Object = new Object();
			data.fileName 	= txtField.text;
			data.key 		= key; 
			this.dispatchEvent( new VListItemSelectEvent ( VListItemSelectEvent.VLIST_ITEM_CLICK, data, true ) );
			bg.removeEventListener( MouseEvent.MOUSE_OUT, mouseHandler );
		}
		public function unSelected(): void
		{
			Tweener.addTween( this, { x: 0, time: 0.5 } );
			bg.addEventListener( MouseEvent.MOUSE_OUT, mouseHandler );
		}
		
		private function mouseHandler ( e: MouseEvent ) : void
		{
			var prevX: int = e.currentTarget.x;
			switch ( e.type )
			{
				case MouseEvent.MOUSE_OVER:
					Tweener.addTween( this, { x: prevX + 10, time: 0.5 } );
					//ballow.visible = true;
				break;
				
				case MouseEvent.MOUSE_OUT:
					Tweener.addTween( this, { x: prevX, time: 0.5 } );
					//ballow.visible = false;
				break;
			}
		}				
	}
}